//
//  WZTaskLogController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/7/1.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZTaskLogController.h"
#import "WZTaskLogCell.h"
#import "WZTaskLog.h"

#define TaskManager_TaskLog_NavigationBar_Title @"任务日志"

@interface WZTaskLogController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *logArray;
@end

@implementation WZTaskLogController

static NSString * const taskLogCell = @"taskLogCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setupTableView];
    
    [self loadDataOfTaskLog];
}


- (void)setupTableView {
    
    self.navigationItem.title = TaskManager_TaskLog_NavigationBar_Title;
    [self.tableView registerNib:[
     UINib nibWithNibName:NSStringFromClass([WZTaskLogCell class]) bundle:nil] forCellReuseIdentifier:taskLogCell];
    
    self.tableView.backgroundColor = WZRGBColor(247, 246, 250);
    self.tableView.rowHeight = 120;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.userInteractionEnabled = YES;
    
}


- (void)loadDataOfTaskLog {
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"name"] = self.oaUserName;
    params[@"pwd"] = self.oaPwd;
    params[@"taskId"] = self.taskId;
    
    [NetWorkShare getTaskManagerLogWithParams:params SuccessHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        if ([responseObject objectForKey:@"success"]) {
            
            self.logArray = [WZTaskLog objectArrayWithKeyValuesArray:responseObject[@"rows"]];
            
            [self.tableView reloadData];
            
            
        } else {
            
            [MBProgressHUD showError:UNKNOWNERROR];
        }
        
    } FailureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NETWORKNERROR];
    }];
}


#pragma mark - tableView 的数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _logArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WZTaskLog *taskLog = _logArray[indexPath.row];
    
    WZTaskLogCell *cell = [tableView dequeueReusableCellWithIdentifier:taskLogCell];
    
    cell.taskLog = taskLog;
    
    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    return 44;
//}

#pragma mark - 懒加载
- (NSMutableArray *)logArray {
    
    if (_logArray == nil) {
        _logArray = [[NSMutableArray alloc] init];
    }
    return _logArray;
}

@end
